package com.yyj;

import com.yyj.web.interceptor.InvokeLogInterceptor;
import com.yyj.web.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


/**
 *
 */
@SpringBootApplication
@ComponentScan("com.yyj")
@ImportResource({ "classpath:spring-config.xml"})
public class PropertyApiMain extends WebMvcConfigurerAdapter{

    @Autowired
    private LoginInterceptor loginInterceptor;

    private static final String excludePaths[] = {"/v2/api-docs" , "/swagger-resources/**"} ;

    public static void main(String[] args) {
        SpringApplication.run(PropertyApiMain.class, args);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        registry.addInterceptor(new InvokeLogInterceptor()).addPathPatterns("/*");
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/auth/**")
                .excludePathPatterns(excludePaths)
        ;
        super.addInterceptors(registry);
    }

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer(){
        return new EmbeddedServletContainerCustomizer() {
                public void customize(ConfigurableEmbeddedServletContainer container) {
                    container.setSessionTimeout(604800);//单位为S 
                }
            };
    }
}
